﻿namespace Jhong.FunMVCBlog.Admin.Controllers
{
    using Jhong.FunMVC;
    using Jhong.FunMVCBlog.Core.Data;
    using Jhong.FunMVCBlog.Core.Entity;
    using Jhong.FunMVCBlog.Core.Infrastructure;
    using System;

    public class CategoryController : SecurityController
    {
        public ActionResult List()
        {
            return View();
        }

        private CategoryData _categoryData = new CategoryData();

        public ActionResult GetData(int pageIndex, int itemsPerPage)
        {
            var data = this._categoryData.GetList(pageIndex, itemsPerPage);
            var page = data.GetPageHelper();
            return AjaxResult(true, data: new { Items = data.Items, Page = page.GetPageHtml() });
        }

        public ActionResult EditCategory(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                ViewData["model"] = new Category();
            }
            else
            {
                var entity = this._categoryData.GetByID(Guid.Parse(id));
                ViewData["model"] = entity;
            }
            return View();
        }

        [HttpPost]
        public ActionResult EditCategory(Category model)
        {
            bool res;
            Guid x = Guid.NewGuid();
            var y = x.ToString();
            model.LastModifyTime = DateTime.Now;
            if (model.ID == Guid.Empty)
            {
                model.CreateTime = DateTime.Now;
                model.ID = Guid.NewGuid();
                model.IsDelete = Core.Enum.DeleteStatus.No;
                res = this._categoryData.Insert(model);
            }
            else
            {
                res = this._categoryData.Update(model);
            }
            return AjaxResult(res, res ? "保存成功" : "保存失败");
        }

        public ActionResult DeleteCategory(Guid id)
        {
            var res = this._categoryData.Delete(new Category() { ID = id });
            return AjaxResult(res, res ? "删除成功" : "删除失败");
        }
    }
}